package com.longsys.framework.eventsourcing.sbclient.config;

import com.longsys.eventsourcing.core.EventAggregate;
import com.longsys.framework.eventsourcing.sbclient.core.EventStoreClient;
import com.longsys.framework.eventsourcing.sbclient.kafka.KafkaEventAggregateMsgProducer;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.KafkaTemplate;

/**
 * @author guozhu.huang
 * @date 2021/9/17 14:58
 */
@Configuration
@AutoConfigureBefore(value = KafkaAutoConfiguration.class)
@EnableConfigurationProperties(value = EsSpringBootClientProperties.class)
public class EsSpringBootClientAutoConfiguration {

    @Bean
    public KafkaEventAggregateMsgProducer kafkaEventAggregateMsgProducer(KafkaTemplate<String, EventAggregate> kafkaTemplate,
                                                                         EsSpringBootClientProperties esSpringBootClientProperties) {
        return new KafkaEventAggregateMsgProducer(kafkaTemplate, esSpringBootClientProperties.getTopic());
    }

    @Bean
    public EventStoreClient eventStoreClient(ApplicationContext applicationContext,
                                             KafkaEventAggregateMsgProducer kafkaEventAggregateMsgProducer,
                                             EsSpringBootClientProperties esSpringBootClientProperties) {
        return new EventStoreClient(applicationContext, kafkaEventAggregateMsgProducer, esSpringBootClientProperties.getSystem());
    }
}
